import pygame
import sys
import time
import random

# 时钟
# 碰撞检测
# 源代码参考 https://blog.csdn.net/zha6476003/article/details/82940350/

pygame.init()
size = width, height = 640, 480
screen = pygame.display.set_mode(size)
color = (0, 0, 0)
ball = pygame.image.load("../res/image/imonilas.png")
ballrect = ball.get_rect()
clock = pygame.time.Clock()
# 设置标题
pygame.display.set_caption("Example program to draw geometry")

speed = [random.randint(0, 10), random.randint(0, 10)]
while True:
    clock.tick(120)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    ballrect = ballrect.move(speed)

    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(color)
    screen.blit(ball, ballrect)
    pygame.display.flip()


pygame.quit()
